package com.dave.serviceribbon.service

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import org.springframework.web.client.RestTemplate

/**
 * Created by mac on 2018/5/17
 * Describes
 */
@Service
class HelloService {
    @Autowired
    internal var restTemplate: RestTemplate? = null

    /**
     * fallbackMethod指定熔断方法
     *
     * @param name
     * @return
     */
    @HystrixCommand(fallbackMethod = "hiError")
    fun hiService(name: String): String? {

        //使用restTemplalte调用rpc服务
        return restTemplate!!.getForObject("http://service-hi/hi?name=$name", String::class.java)


    }

    /**
     * 当服务出问题了的时候调用熔断方法
     *
     * @param name
     * @return
     */
    fun hiError(name: String): String {
        return "hi,$name,sorry,error!"
    }
}
